define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    $.widget('rk.groupPanelTableCtrl', $.rk.oaPanelCtrl, {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me.loaddata();
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var type = opt.type;
            elem.on("click", "[act='group_table_row']", function(){
                var row = $(this);
                var gid = row.attr("gid");
                var uid = row.attr("uid");
                var param = {type:"group",gid:gid,uid:uid,loadParam:me.loadParam};
                var permission = row.attr("permission");
                var isMember = row.attr("isMember");
                var showRowHover = function(){
                    elem. find( "[act='group_table_row']"). removeClass( "show-content-hover");
                    row. addClass( "show-content-hover" );
                };
                if(permission != "1" ){
                    me._showDetails(param,"middle");
                    showRowHover();
                }else if(isMember){
                    me._showDetails(param,"middle");
                    showRowHover();
                }else{
                    rk.noticeError(rk.i18n('COLLEAGUE_NO_AUTHORITY_ERROR'));
                }
            });
            elem.on('mouseover', "[act='group_one_join_out']", function(){
                var button = $(this);
                var ismember = button.attr("ismember");
                if(ismember == 1) {
                    button.html(rk.i18n('GROUP_SIGN_OUT'));
                }
            });
            elem.on('mouseout', "[act='group_one_join_out']", function(){
                var button = $(this);
                var ismember = button.attr("ismember");
                if(ismember == 1) {
                    button.html(rk.i18n('GROUP_HAVE_JOIN'));
                }
            });
            elem.on("click", "[act='group_one_join_out']", function(e){
                e.preventDefault();
                var button = $(this);
                var isMember = button.attr("isMember");
                var uid = button.attr("uid");
                var gid = button.attr("gid");
                if(uid && gid) {
                    if(button.hasClass('disable')) return;
                    button.addClass('disable');
                    if(isMember == "1") {
                        rk.confirm(rk.i18n('GROUP_EXIT_CONFIRM'), function () {
                            rk.post("/json/oa_group/member-quit.action", {uid: uid, gid: gid}, function (json) {
                                if(json.status === 0){
                                    if(type != "all_group"){
                                        me.loaddata(type);
                                    }else {
                                        button.attr("isMember", "0");
                                        button.removeClass("group_button_in");
                                        button.addClass("group_button_join");
                                        button.html(rk.i18n('GROUP_JOIN'));
                                    }
                                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                                }else{
                                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                                }
                                button.removeClass('disable');
                            });
                        }, function () {
                            button.removeClass('disable');//取消
                        });

                    }else if(isMember == "0"){
                        rk.post("/json/oa_group/member-join.action", {uid: uid, gid: gid}, function (json) {
                            if(json.status === 0){
                                button.attr("isMember", "1");
                                button.removeClass("group_button_join");
                                button.addClass("group_button_in");
                                button.html(rk.i18n('GROUP_HAVE_JOIN'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            }else{
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            button.removeClass('disable');
                        });
                    }
                }
                return false;
            });
        },
        _showDetails: function(param,size){
            var me = this;
            me.popupContent({
                size: size,
                onSuccess: function (contentElem) {
                    /*require.async(["./colleagueDepartContentCtrl"], function () {
                        contentElem.colleagueDepartContentCtrl(param);
                    });*/
                    require.async(["./groupContentCtrl"],function(){
                        contentElem.groupContentCtrl(param);
                    });
                }
            });
        },
        loaddata:function(param){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var type = opt.type;
            if(!param) {
                param = {pageNo: 1, pageSize: 20};
            }
            if(type && me.loadDataJson[type]){
                var loadParam = {
                    type:type,
                    param:param
                };
                me.loadParam = loadParam;
                $.proxy(me.loadDataJson[type].call(this,param),me);
            }
        },
        loadParam:"",
        loadDataJson:{
            "all_group":function(param){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                elem.find("[act='group_page_parent']").hide();
                rk.post("/json/oa_group/work-groups.action",param,function(json){
                    if(json.status === 0) {
                        if(json.groups && json.groups.length > 0) {
                            var temp = require("oa/tmpl/group/tpl_group_panel_table_rows.tpl");
                            var html = rk.templateText(temp, json);
                            elem.html(html);
                            elem.find("[act='group_page_parent']").show();
                            elem.find("[act='group_page']").rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(params);
                                }
                            });
                        }else{
                            var temp = require("oa/tmpl/group/tpl_group_none.tpl");
                            var html = rk.templateText(temp, {text:rk.i18n('GROUP_NO_PLEASE_CREATE')});
                            elem.html(html);
                        }
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            },
            "my_group":function(param){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                rk.post("/json/oa_group/joined-work-groups.action",param,function(json){
                    if(json.status === 0) {
                        if(json.groups && json.groups.length > 0) {
                            var temp = require("oa/tmpl/group/tpl_group_panel_table_rows.tpl");
                            var html = rk.templateText(temp, json);
                            elem.html(html);
                            elem.find("[act='group_page']").rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(params)
                                }
                            });
                        }else{
                            var temp = require("oa/tmpl/group/tpl_group_none.tpl");
                            var html = rk.templateText(temp, {text:rk.i18n('GROUP_NO_CHECK_ALL')});
                            elem.html(html);
                        }
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            },
            "my_create_group":function(param){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                rk.post("/json/oa_group/admin-work-groups.action",param,function(json){
                    if(json.status === 0) {
                        if(json.groups && json.groups.length > 0) {
                            var temp = require("oa/tmpl/group/tpl_group_panel_table_rows.tpl");
                            var html = rk.templateText(temp, json);
                            elem.html(html);
                            elem.find("[act='group_page']").rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(params)
                                }
                            });
                        }else{
                            var temp = require("oa/tmpl/group/tpl_group_none.tpl");
                            var html = rk.templateText(temp, {text:rk.i18n('GROUP_NO_PLEASE_CREATE')});
                            elem.html(html);
                        }
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            }
        }
    });
});